<template>
  <div class="allotUser" @click="$event.stopPropagation()">
    <div class="box">
      <div class="title">
        <h2>分配用户</h2>
        <el-icon @click="stateF"><Close /></el-icon>
      </div>
      <div class="inp">
        <div>
          当前用户:
          <span>{{ form.name }}</span>
        </div>
        <div>
          当前角色:
          <span>{{ form.role }}</span>
        </div>
        <el-form :model="form" :rules="rules" ref="formRef">
          <el-form-item label="Activity zone" prop="tit">
            <el-select v-model="form.tit" placeholder="请选择" size="large">
              <el-option label="超级管理员" value="超级管理员" />
              <el-option label="主管" value="主管" />
              <el-option label="测试角色" value="测试角色" />
              <el-option label="40" value="40" />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div class="but">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="ensure">确定</el-button>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { Close } from '@element-plus/icons-vue'
import { useCounterStoreB } from '@/stores/temporary'
import { getAllotUser } from '@/api/allotUser'
const storeB = useCounterStoreB()
const form = reactive({
  tit: '请选择',
  role: '',
  name: '',
  id: 0,
})
const rules = reactive({
  tit: [{ required: true, message: '请选择活动区域', trigger: 'change' }],
})
const state = ref()
const stateF = async () => {
  close()
}
const close = () => {
  state.value = !state.value
  storeB.stateFun(state.value)
}
const ensure = async () => {
  try {
    const res = await getAllotUser(form.id, form.tit)
    console.log(res)
  } catch (err) {
    console.log(err)
  }
  close()
}

watch(
  () => storeB.addState,
  (newVal) => {
    state.value = newVal
  },
)
watch(
  () => storeB.rowObject.role,
  (newVal) => {
    form.role = newVal
  },
)
watch(
  () => storeB.rowObject.name,
  (newVal) => {
    form.name = newVal
  },
)
watch(
  () => storeB.rowObject.id,
  (newVal) => {
    form.id = newVal
  },
)
</script>

<style scoped lang="scss">
.box {
  width: 50vw;
  background: #fff;
  padding: 10px;
  .title {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    h2 {
      margin: 0;
    }
  }
  .but {
    width: 100%;
    display: flex;
    justify-content: flex-end;
    align-items: center;
  }
}
</style>
